/**
 * @author liao / 110233853@qq.com
 */
THREE.PanoramaControls = function(camera){
	document.addEventListener('mousedown',onDocumentMouseDown,false);
	document.addEventListener('mousemove',onDocumentMouseMove,false);
	document.addEventListener('mouseup',onDocumentMouseUp,false);
	document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
	document.addEventListener( 'DOMMouseScroll', onDocumentMouseWheel, false);
	document.addEventListener('touchstart', touchStart,false);
	document.addEventListener('touchmove', touchMove,false);
	document.addEventListener('touchend', touchEnd,false);

	var isUserInteracting = false,
	onPointerDownPointerX=0,
	onPointerDownPointerY=0,
	onPointerDownLon=0,
	onPointerDownLat=0;

	this.lon=0;
	this.lat=0;

	this.enabled = true;

	var scope = this;


	this.setLatNLon = function(lat,lon){
		scope.lat = lat;
		scope.lon = lon;
		update();
	};

	function update (){
		scope.lat = Math.max( - 85, Math.min( 85, scope.lat ) );
		phi = THREE.Math.degToRad( 90 - scope.lat );
		theta = THREE.Math.degToRad( scope.lon );
		var vector = new THREE.Vector3();
		vector.x = 500 * Math.sin( phi ) * Math.cos( theta );
		vector.y = 500 * Math.cos( phi );
		vector.z = 500 * Math.sin( phi ) * Math.sin( theta );
		camera.lookAt(vector);
	}

	/*
	 * TODO: add touch event handlers.
	 */
	function touchStart(event){
		event.preventDefault();
		switch(event.touches.length){
			case 1:
				rotateStart(event.touches[0].clientX,event.touches[0].clientY);
				break;
			case 2:
				break;
			default:
				break;
		}
	}
	function touchMove(event){
		event.preventDefault();
		switch(event.touches.length){
			case 1:
				rotateMove(event.touches[0].clientX,event.touches[0].clientY);
				break;
			case 2:
				break;
			default:
				break;
		}
	}

	function touchEnd(event){
		event.preventDefault();
		rotateEnd(event.clientX,event.clientY);
	}

	function rotateStart(x,y){
		if(scope.enabled === false)
		{
			return;
		}
		isUserInteracting = true;
		onPointerDownPointerX = x;
		onPointerDownPointerY = y;
		onPointerDownLon = scope.lon;
		onPointerDownLat = scope.lat;
	}

	function onDocumentMouseDown(event){
		rotateStart(event.clientX,event.clientY);
	}

	function rotateMove(x,y){
		if(scope.enabled === false)
		{
			return;
		}
		if(isUserInteracting){
			scope.lon = ( onPointerDownPointerX - x) * 0.1 + onPointerDownLon;
			scope.lat = ( y - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
			console.log("lat:" + scope.lat + " lon:" + scope.lon);
			update();
		}
	}

	function onDocumentMouseMove(event){
		rotateMove(event.clientX,event.clientY);
	}

	function rotateEnd(x,y){
		if(scope.enabled === false)
		{
			return;
		}
		isUserInteracting = false;
	}

	function onDocumentMouseUp( event ){
		rotateEnd(event.clientX,event.clientY);
	}

	function onDocumentMouseWheel( event ) {
		console.log(camera.fov);
		if(scope.enabled === false)
		{
			return;
		}
		event.preventDefault();
		// WebKit
		var delta = 0;
		if ( event.wheelDeltaY ) {
			delta = -event.wheelDeltaY * 0.05;
			// Opera / Explorer 9
		} else if ( event.wheelDelta ) {
			delta = -event.wheelDelta * 0.05;
			// Firefox
		} else if ( event.detail ) {
			delta = event.detail * 1.0;
		}
		if(camera.fov + delta <= 100 && camera.fov + delta >=55){
			camera.fov += delta;
			camera.updateProjectionMatrix();
		}
		// TODO : find the initial val of camera.fov
	}
};
